package com.rocksdata.mc.ui.fragment;


import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ctz.commonframe.constant.DeviceInfo;
import com.ctz.commonframe.ui.BaseFragment;
import com.ctz.commonframe.ui.view.EmbeddedListView;
import com.ctz.commonframe.ui.widget.AbsAdapter;
import com.ctz.commonframe.ui.widget.AdapterViewHolder;
import com.ctz.commonframe.utils.DateUtil;
import com.ctz.commonframe.utils.DensityUtil;
import com.rocksdata.mc.entity.WeekTimetableInfo;
import com.rocksdata.mc.R;
import com.rocksdata.mc.business.UserInfoManager;
import com.rocksdata.mc.entity.CourseInfo;
import com.rocksdata.mc.entity.UserInfo;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by 陈堂正 on 2017/2/13.
 */

@ContentView(R.layout.fragment_week_timetable)
public class WeekTimetableFragment extends BaseFragment {
    private static final int WIDTH_LEFT_LIST = 30;//dp
    private static final int HEIGHT_TOP_LIST = 45;//dp
    private int formStep; //正方形表格的边长

    @ViewInject(R.id.layout_date1)
    private LinearLayout layoutDate1;
    @ViewInject(R.id.layout_date2)
    private LinearLayout layoutDate2;
    @ViewInject(R.id.layout_date3)
    private LinearLayout layoutDate3;
    @ViewInject(R.id.layout_date4)
    private LinearLayout layoutDate4;
    @ViewInject(R.id.layout_date5)
    private LinearLayout layoutDate5;
    @ViewInject(R.id.tv_date1)
    private TextView tvDate1;
    @ViewInject(R.id.tv_date2)
    private TextView tvDate2;
    @ViewInject(R.id.tv_date3)
    private TextView tvDate3;
    @ViewInject(R.id.tv_date4)
    private TextView tvDate4;
    @ViewInject(R.id.tv_date5)
    private TextView tvDate5;
    @ViewInject(R.id.lv_index_morning)
    private EmbeddedListView lvIndexMorning;
    @ViewInject(R.id.lv_index_afternoon)
    private EmbeddedListView lvIndexAfternoon;
    @ViewInject(R.id.lv_index_evening)
    private EmbeddedListView lvIndexEvening;
    @ViewInject(R.id.lv_timetable1_morning)
    private EmbeddedListView lvTimetable1Morning;
    @ViewInject(R.id.lv_timetable2_morning)
    private EmbeddedListView lvTimetable2Morning;
    @ViewInject(R.id.lv_timetable3_morning)
    private EmbeddedListView lvTimetable3Morning;
    @ViewInject(R.id.lv_timetable4_morning)
    private EmbeddedListView lvTimetable4Morning;
    @ViewInject(R.id.lv_timetable5_morning)
    private EmbeddedListView lvTimetable5Morning;
    @ViewInject(R.id.lv_timetable1_afternoon)
    private EmbeddedListView lvTimetable1Afternoon;
    @ViewInject(R.id.lv_timetable2_afternoon)
    private EmbeddedListView lvTimetable2Afternoon;
    @ViewInject(R.id.lv_timetable3_afternoon)
    private EmbeddedListView lvTimetable3Afternoon;
    @ViewInject(R.id.lv_timetable4_afternoon)
    private EmbeddedListView lvTimetable4Afternoon;
    @ViewInject(R.id.lv_timetable5_afternoon)
    private EmbeddedListView lvTimetable5Afternoon;
    @ViewInject(R.id.lv_timetable1_evening)
    private EmbeddedListView lvTimetable1Evening;
    @ViewInject(R.id.lv_timetable2_evening)
    private EmbeddedListView lvTimetable2Evening;
    @ViewInject(R.id.lv_timetable3_evening)
    private EmbeddedListView lvTimetable3Evening;
    @ViewInject(R.id.lv_timetable4_evening)
    private EmbeddedListView lvTimetable4Evening;
    @ViewInject(R.id.lv_timetable5_evening)
    private EmbeddedListView lvTimetable5Evening;

    private String[] indexMorning = {"1", "2", "3", "4"};
    private String[] indexAfternoon = {"5", "6", "7", "8"};
    private String[] indexEvening = {"9", "10"};

    private List<CourseInfo> d1CourseList = new ArrayList<>();
    private List<CourseInfo> d2CourseList  = new ArrayList<>();
    private List<CourseInfo> d3CourseList  = new ArrayList<>();
    private List<CourseInfo> d4CourseList  = new ArrayList<>();
    private List<CourseInfo> d5CourseList  = new ArrayList<>();

    private TimetableInfoListAdapter adapter1Morning;
    private TimetableInfoListAdapter adapter1Afternoon;
    private TimetableInfoListAdapter adapter1Evening;
    private TimetableInfoListAdapter adapter2Morning;
    private TimetableInfoListAdapter adapter2Afternoon;
    private TimetableInfoListAdapter adapter2Evening;
    private TimetableInfoListAdapter adapter3Morning;
    private TimetableInfoListAdapter adapter3Afternoon;
    private TimetableInfoListAdapter adapter3Evening;
    private TimetableInfoListAdapter adapter4Morning;
    private TimetableInfoListAdapter adapter4Afternoon;
    private TimetableInfoListAdapter adapter4Evening;
    private TimetableInfoListAdapter adapter5Morning;
    private TimetableInfoListAdapter adapter5Afternoon;
    private TimetableInfoListAdapter adapter5Evening;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //边长为(屏幕宽度 - 左侧标题宽度) / 5
        formStep = (DeviceInfo.screenWidth - DensityUtil.dip2px(mContext, WIDTH_LEFT_LIST)) / 5;
    }

    public void setDatas(WeekTimetableInfo info){
//        d1CourseList.addAll(JSONArray.parseArray(info.getD1(), CourseInfo.class));
//        d2CourseList.addAll(JSONArray.parseArray(info.getD2(), CourseInfo.class));
//        d3CourseList.addAll(JSONArray.parseArray(info.getD3(), CourseInfo.class));
//        d4CourseList.addAll(JSONArray.parseArray(info.getD4(), CourseInfo.class));
//        d5CourseList.addAll(JSONArray.parseArray(info.getD5(), CourseInfo.class));
        if(adapter1Morning != null)
            adapter1Morning.notifyDataSetChanged();
        if(adapter1Afternoon != null)
            adapter1Afternoon.notifyDataSetChanged();
        if(adapter1Evening != null)
            adapter1Evening.notifyDataSetChanged();
        if(adapter2Morning != null)
            adapter2Morning.notifyDataSetChanged();
        if(adapter2Afternoon != null)
            adapter2Afternoon.notifyDataSetChanged();
        if(adapter2Evening != null)
            adapter2Evening.notifyDataSetChanged();
        if(adapter3Morning != null)
            adapter3Morning.notifyDataSetChanged();
        if(adapter3Afternoon != null)
            adapter3Afternoon.notifyDataSetChanged();
        if(adapter3Evening != null)
            adapter3Evening.notifyDataSetChanged();
        if(adapter4Morning != null)
            adapter4Morning.notifyDataSetChanged();
        if(adapter4Afternoon != null)
            adapter4Afternoon.notifyDataSetChanged();
        if(adapter4Evening != null)
            adapter4Evening.notifyDataSetChanged();
        if(adapter5Morning != null)
            adapter5Morning.notifyDataSetChanged();
        if(adapter5Afternoon != null)
            adapter5Afternoon.notifyDataSetChanged();
        if(adapter5Evening != null)
            adapter5Evening.notifyDataSetChanged();

    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        int dayOfWeek = DateUtil.getDayOfWeekIndex(System.currentTimeMillis());
        List<TextView> dateTextViewList = new ArrayList<>();
        dateTextViewList.add(tvDate1);
        dateTextViewList.add(tvDate2);
        dateTextViewList.add(tvDate3);
        dateTextViewList.add(tvDate4);
        dateTextViewList.add(tvDate5);
        for(int i = 0; i < dateTextViewList.size(); i++){
            long millis = System.currentTimeMillis() + (i - dayOfWeek + 1) * DateUtil.MILLIS_PER_DAY;
            dateTextViewList.get(i).setText(DateUtil.millis2String(millis, "MM/dd"));
        }

        switch (dayOfWeek) {
            case 1:
                layoutDate1.setBackgroundColor(getResources().getColor(android.R.color.holo_orange_dark));
                break;
            case 2:
                layoutDate2.setBackgroundColor(getResources().getColor(android.R.color.holo_orange_dark));
                break;
            case 3:
                layoutDate3.setBackgroundColor(getResources().getColor(android.R.color.holo_orange_dark));
                break;
            case 4:
                layoutDate4.setBackgroundColor(getResources().getColor(android.R.color.holo_orange_dark));
                break;
            case 5:
            case 6:
            case 0:
                layoutDate5.setBackgroundColor(getResources().getColor(android.R.color.holo_orange_dark));
                break;
        }

        lvIndexMorning.setAdapter(new IndexListAdapter(mContext, indexMorning));
        lvIndexAfternoon.setAdapter(new IndexListAdapter(mContext, indexAfternoon));
        lvIndexEvening.setAdapter(new IndexListAdapter(mContext, indexEvening));

//        adapter1Morning = new TimetableInfoListAdapter(mContext, d1CourseList, CourseInfo.DAY_ZONE_MORNING);
//        adapter1Afternoon = new TimetableInfoListAdapter(mContext, d1CourseList, CourseInfo.DAY_ZONE_AFTERNOON);
//        adapter1Evening = new TimetableInfoListAdapter(mContext, d1CourseList, CourseInfo.DAY_ZONE_EVENING);
//        adapter2Morning = new TimetableInfoListAdapter(mContext, d2CourseList, CourseInfo.DAY_ZONE_MORNING);
//        adapter2Afternoon = new TimetableInfoListAdapter(mContext, d2CourseList, CourseInfo.DAY_ZONE_AFTERNOON);
//        adapter2Evening = new TimetableInfoListAdapter(mContext, d2CourseList, CourseInfo.DAY_ZONE_EVENING);
//        adapter3Morning = new TimetableInfoListAdapter(mContext, d3CourseList, CourseInfo.DAY_ZONE_MORNING);
//        adapter3Afternoon = new TimetableInfoListAdapter(mContext, d3CourseList, CourseInfo.DAY_ZONE_AFTERNOON);
//        adapter3Evening = new TimetableInfoListAdapter(mContext, d3CourseList, CourseInfo.DAY_ZONE_EVENING);
//        adapter4Morning = new TimetableInfoListAdapter(mContext, d4CourseList, CourseInfo.DAY_ZONE_MORNING);
//        adapter4Afternoon = new TimetableInfoListAdapter(mContext, d4CourseList, CourseInfo.DAY_ZONE_AFTERNOON);
//        adapter4Evening = new TimetableInfoListAdapter(mContext, d4CourseList, CourseInfo.DAY_ZONE_EVENING);
//        adapter5Morning = new TimetableInfoListAdapter(mContext, d5CourseList, CourseInfo.DAY_ZONE_MORNING);
//        adapter5Afternoon = new TimetableInfoListAdapter(mContext, d5CourseList, CourseInfo.DAY_ZONE_AFTERNOON);
//        adapter5Evening = new TimetableInfoListAdapter(mContext, d5CourseList, CourseInfo.DAY_ZONE_EVENING);

        lvTimetable1Morning.setAdapter(adapter1Morning);
        lvTimetable1Afternoon.setAdapter(adapter1Afternoon);
        lvTimetable1Evening.setAdapter(adapter1Evening);
        lvTimetable2Morning.setAdapter(adapter2Morning);
        lvTimetable2Afternoon.setAdapter(adapter2Afternoon);
        lvTimetable2Evening.setAdapter(adapter2Evening);
        lvTimetable3Morning.setAdapter(adapter3Morning);
        lvTimetable3Afternoon.setAdapter(adapter3Afternoon);
        lvTimetable3Evening.setAdapter(adapter3Evening);
        lvTimetable4Morning.setAdapter(adapter4Morning);
        lvTimetable4Afternoon.setAdapter(adapter4Afternoon);
        lvTimetable4Evening.setAdapter(adapter4Evening);
        lvTimetable5Morning.setAdapter(adapter5Morning);
        lvTimetable5Afternoon.setAdapter(adapter5Afternoon);
        lvTimetable5Evening.setAdapter(adapter5Evening);

        super.onViewCreated(view, savedInstanceState);
    }

    private class IndexListAdapter extends AbsAdapter<String>{

        public IndexListAdapter(Context context, String[] datas) {
            super(context, Arrays.asList(datas), R.layout.item_index_list);
        }

        @Override
        public void convert(AdapterViewHolder holder, String data) {
            TextView tvIndex = holder.getView(R.id.tv_index);
            tvIndex.setText(data);
        }
    }

    private class TimetableInfoListAdapter extends BaseAdapter{

        private int dayZone;
        private List<CourseInfo> dataList;
        private List<CourseInfo> sameList = new ArrayList<CourseInfo>();
        private Context context;

        public TimetableInfoListAdapter(Context context, List<CourseInfo> dataList, int dayZone) {
            this.context = context;
            this.dataList = dataList;
            this.dayZone = dayZone;
//            for(CourseInfo info: dataList){
//                if(info.duration > 1){
//                    sameList.add(info);
//                }
//            }
        }

        @Override
        public int getCount() {
//            if(dayZone == CourseInfo.DAY_ZONE_EVENING){
//                return 2;
//            }
            return 4;
        }

        @Override
        public CourseInfo getItem(int position) {
            CourseInfo courseInfo = null;
            for(CourseInfo info: dataList){
                if(info.getLesson() == (dayZone - 1) * 4 + position + 1){
                    courseInfo = info;
                    break;
                }
            }

            return courseInfo;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if(convertView == null){
                holder = new ViewHolder();
                convertView = LayoutInflater.from(context).inflate(R.layout.item_timetable_info_list, null);
                holder.layoutParent = (RelativeLayout) convertView.findViewById(R.id.layout_parent);
                holder.tvCourseName = (TextView) convertView.findViewById(R.id.tv_course_name);
                holder.ivStatus = (ImageView) convertView.findViewById(R.id.iv_course_status);
                holder.flagSameCourse = (ImageView) convertView.findViewById(R.id.iv_flag_same_course);

                convertView.setTag(holder);
            }else{
                holder = (ViewHolder) convertView.getTag();
            }
            final CourseInfo info = getItem(position);
            if(info == null){
                holder.tvCourseName.setText("");
                holder.ivStatus.setVisibility(View.INVISIBLE);
            }else{
                if(info.getChange() == CourseInfo.STATUS_TEST){
                    holder.tvCourseName.setText("考试");
                    holder.tvCourseName.setTextColor(context.getResources().getColor(android.R.color.holo_orange_dark));
                }else {
                    holder.tvCourseName.setText(info.getCourseName());
                    holder.tvCourseName.setTextColor(Color.BLACK);
                }
                switch (info.getChange()) {
                    case CourseInfo.STATUS_CHANGE_TEACHER:
                        holder.ivStatus.setVisibility(View.VISIBLE);
                        holder.ivStatus.setImageResource(R.mipmap.ic_teacher_changed);
                        break;
                    case CourseInfo.STATUS_CHANGE_COURSE:
                        holder.ivStatus.setVisibility(View.VISIBLE);
                        holder.ivStatus.setImageResource(R.mipmap.ic_course_changed);
                        break;
                    default:
                        holder.ivStatus.setVisibility(View.INVISIBLE);
                        break;
                }
                boolean isSameCourse = false;
                int userType = UserInfoManager.getCurrentUserType();
                CourseInfo nextCourse = getItem(position + 1);
                CourseInfo preCourse = position > 0 ? getItem(position - 1) : null;

                if(nextCourse != null){
                    if(userType == UserInfo.USER_TYPE_STUDENT && nextCourse.getCourseCode().equals(info.getCourseCode())){
                        isSameCourse = true;
                    }
                    if(userType == UserInfo.USER_TYPE_TEACHER && nextCourse.getCourseCode().equals(info.getCourseCode()) && nextCourse.getClassCode().equals(info.getClassCode())){
                        isSameCourse = true;
                    }
                }
                if(preCourse != null){
                    if(userType == UserInfo.USER_TYPE_STUDENT && preCourse.getCourseCode().equals(info.getCourseCode())){
                        isSameCourse = true;
                    }
                    if(userType == UserInfo.USER_TYPE_TEACHER && preCourse.getCourseCode().equals(info.getCourseCode()) && preCourse.getClassCode().equals(info.getClassCode())){
                        isSameCourse = true;
                    }
                }

                holder.flagSameCourse.setVisibility(isSameCourse ? View.VISIBLE: View.INVISIBLE);
            }

            return convertView;
        }

        class ViewHolder{
            RelativeLayout layoutParent;
            TextView tvCourseName;
            ImageView ivStatus;
            ImageView flagSameCourse;
        }
    }
}
